package serverInstallation;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import serverManger.ServerInfo;

import com.cloudgarden.resource.SWTResourceManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class InstallServer extends org.eclipse.swt.widgets.Composite {

	private Button doneButton;
	private Text dbPasswordText;
	private Label dbPasswordLabel;
	private Text dbUserNameText;
	private Label dbUserNameLabel;
	private Button browseButton;
	private Text locationText;
	private Label locationLabel;
	private Label logoLabel;

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	public InstallServer(Composite parent, int style) {
		super(parent, style);
		initGUI();
	}
	
	/**
	* Initializes the GUI.
	*/
	private void initGUI() {
		try {
			this.setSize(495, 272);
			this.getShell().setText("CleverDropBox - server installation");
			//this.setBackground(SWTResourceManager.getColor(255, 255, 255));
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				doneButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData doneButtonLData = new FormData();
				doneButtonLData.left =  new FormAttachment(0, 1000, 212);
				doneButtonLData.top =  new FormAttachment(0, 1000, 240);
				doneButtonLData.width = 41;
				doneButtonLData.height = 26;
				doneButton.setLayoutData(doneButtonLData);
				doneButton.setText("Done");
				doneButton.setEnabled(false);
				doneButton.addSelectionListener(new SelectionListener() {
					
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						CreatXMLFile creatXMLFile = new CreatXMLFile();
						creatXMLFile.createDbAndHibernateXml("serverDataBase", dbUserNameText.getText(), dbPasswordText.getText());
						done();
						
					}
					
					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {}
				});
			}
			{
				FormData dbPasswordTextLData = new FormData();
				dbPasswordTextLData.left =  new FormAttachment(0, 1000, 169);
				dbPasswordTextLData.top =  new FormAttachment(0, 1000, 199);
				dbPasswordTextLData.width = 169;
				dbPasswordTextLData.height = 23;
				dbPasswordText = new Text(this, SWT.NONE | SWT.BORDER);
				dbPasswordText.setLayoutData(dbPasswordTextLData);
				dbPasswordText.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						KeyReleased(evt);
					}
				});
			}
			{
				dbPasswordLabel = new Label(this, SWT.NONE);
				FormData dbPasswordLabelLData = new FormData();
				dbPasswordLabelLData.left =  new FormAttachment(0, 1000, 169);
				dbPasswordLabelLData.top =  new FormAttachment(0, 1000, 174);
				dbPasswordLabelLData.width = 188;
				dbPasswordLabelLData.height = 19;
				dbPasswordLabel.setLayoutData(dbPasswordLabelLData);
				dbPasswordLabel.setText("DataBase password:");
			}
			{
				FormData dbUserNameTextLData = new FormData();
				dbUserNameTextLData.left =  new FormAttachment(0, 1000, 169);
				dbUserNameTextLData.top =  new FormAttachment(0, 1000, 134);
				dbUserNameTextLData.width = 172;
				dbUserNameTextLData.height = 22;
				dbUserNameText = new Text(this, SWT.NONE | SWT.BORDER);
				dbUserNameText.setLayoutData(dbUserNameTextLData);
				dbUserNameText.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						KeyReleased(evt);
					}
				});
			}
			{
				dbUserNameLabel = new Label(this, SWT.NONE);
				FormData dbUserNameLabelLData = new FormData();
				dbUserNameLabelLData.left =  new FormAttachment(0, 1000, 169);
				dbUserNameLabelLData.top =  new FormAttachment(0, 1000, 112);
				dbUserNameLabelLData.width = 179;
				dbUserNameLabelLData.height = 22;
				dbUserNameLabel.setLayoutData(dbUserNameLabelLData);
				dbUserNameLabel.setText("DataBase user name:");
			}
			{
				browseButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData browseButtonLData = new FormData();
				browseButtonLData.left =  new FormAttachment(0, 1000, 361);
				browseButtonLData.top =  new FormAttachment(0, 1000, 70);
				browseButtonLData.width = 59;
				browseButtonLData.height = 31;
				browseButton.setLayoutData(browseButtonLData);
				browseButton.setText("Browse...");
				browseButton.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						browseButtonWidgetSelected(evt);
					}
				});
			}
			{
				FormData locationTextLData = new FormData();
				locationTextLData.left =  new FormAttachment(0, 1000, 169);
				locationTextLData.top =  new FormAttachment(0, 1000, 70);
				locationTextLData.width = 168;
				locationTextLData.height = 24;
				locationText = new Text(this, SWT.NONE | SWT.BORDER);
				locationText.setLayoutData(locationTextLData);
				locationText.setFocus();
				locationText.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						KeyReleased(evt);
					}
				});
			}
			{
				locationLabel = new Label(this, SWT.NONE);
				FormData locationLabelLData = new FormData();
				locationLabelLData.left =  new FormAttachment(0, 1000, 169);
				locationLabelLData.top =  new FormAttachment(0, 1000, 28);
				locationLabelLData.width = 149;
				locationLabelLData.height = 36;
				locationLabel.setLayoutData(locationLabelLData);
				locationLabel.setText("Please choose location\nfor all CleverDropBox files:");
			}
			//the logo label
			{
				logoLabel = new Label(this, SWT.NONE);
				FormData logoLabelLData = new FormData();
				logoLabelLData.left =  new FormAttachment(0, 1000, 12);
				logoLabelLData.top =  new FormAttachment(0, 1000, 34);
				logoLabelLData.width = 136;
				logoLabelLData.height = 154;
				logoLabel.setLayoutData(logoLabelLData);
				logoLabel.setText("here we \nneed to \nput logo\n");
				logoLabel.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));
				logoLabel.setLocation(new org.eclipse.swt.graphics.Point(500, 200));
				logoLabel.setImage(new Image(this.getDisplay(), "tour6.png"));

			}
			

			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void done() {
		CreatXMLFile creatXMLFile = new CreatXMLFile();
		creatXMLFile.createDbAndHibernateXml("ServerDatabase", dbUserNameText.getText(), dbPasswordText.getText());
		
		ServerInfo serverInfo = ServerInfo.getServerInfoIns();
		serverInfo.setDbPassword(dbPasswordText.getText());
		serverInfo.setDbUserName(dbUserNameText.getText());
		serverInfo.setFolderLocation(locationText.getText());
		this.getShell().dispose();
		
		
	}

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		InstallServer inst = new InstallServer(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	public static void openGUI(){
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		InstallServer inst = new InstallServer(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
		
	
	private void browseButtonWidgetSelected(SelectionEvent evt) {
		DirectoryDialog dlg = new DirectoryDialog(this.getShell());
		// Change the title bar text
        dlg.setText("DirectoryDialog");

        // Customizable message displayed in the dialog
        dlg.setMessage("Select a directory:");

        // Calling open() will open and run the dialog.
        // It will return the selected directory, or
        // null if user cancels
        String dir = dlg.open();
        if (dir != null) {
          // Set the text box to the new selection
          locationText.setText(dir);
          KeyReleased(null);
          
        }
	}
	
	private void KeyReleased(KeyEvent evt) {
		if(locationText.getText().compareTo("")!=0 && dbUserNameText.getText().compareTo("")!=0 && dbPasswordText.getText().compareTo("")!=0)
			doneButton.setEnabled(true);
		else
			doneButton.setEnabled(false);
	}

}
